//
//  LGConfirmTableController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2025/1/14.
//

#import "LGConfirmTableController.h"
#import "LGOrderConfirmTableCell.h"
@interface LGConfirmTableController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *pageNo;
/**   */
@property (nonatomic , copy)NSString *keyword;

@end

@implementation LGConfirmTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.searchTF];
    [self.view addSubview:self.tableView];
    [self requestData];
}

-(void)viewWillAppearAction{
    if ([self.type isEqualToString:@"2"]) {
        [self requestData];
    }
}

-(void)requestData{
    self.pageNo = @"1";
    self.dataArray = [NSMutableArray array];
    [self requestDataWithPage:@"1"];
}

-(void)requestMoreData{
    [self requestDataWithPage:self.pageNo];
}

-(void)requestDataWithPage:(NSString *)pageNo{
    __block NSString *currentPage = pageNo;
    self.keyword = (_keyword && ![_keyword isEmptyString])?_keyword:@"";
    [RequestUtil withPOST:@"/doctor/order/pagedList" parameters:@{@"pageNo":self.pageNo,@"pageSize":@"20",@"order_type":@"10",@"data_type":self.type,@"client":@"APP",@"keyword":self.keyword} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if([currentPage isEqualToString:@"1"]){
            [self.dataArray removeAllObjects];
        }
        if(code == 200){
            //总数
            NSInteger total = [LGNSString(returnData[@"total"]) integerValue];
            if (self.delegate && [self.delegate respondsToSelector:@selector(totalNumberChanged:type:)]) {
                [self.delegate totalNumberChanged:total type:self.type];
            }
            
            NSArray *tempArray = returnData[@"data"];
            if(tempArray.count>0){
                self.pageNo = [NSString stringWithFormat:@"%ld",[returnData[@"current_page"] integerValue]+1];
                [self.dataArray addObjectsFromArray:tempArray];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        BOOL needSearch = self.searchTF.isFirstResponder;
        
        [self.tableView reloadData];
        if (needSearch == YES) {
            [self.searchTF becomeFirstResponder];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    NSString *lang = textField.textInputMode.primaryLanguage;// 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textField markedTextRange];
        if (!selectedRange) { // 没有高亮
            self.keyword = textField.text;
            [self requestData];
        }
    }else{
        self.keyword = textField.text;
        [self requestData];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGOrderConfirmTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.searchTF resignFirstResponder];
    if ([self.type isEqualToString:@"1"]) {
        NSDictionary *tempDic = self.dataArray[indexPath.row];
        __block NSString *orderid = LGNSString(tempDic[@"order_id"]);
        __block NSInteger index = indexPath.row;
        __weak typeof(self) weakSelf = self;
        [LGAlertTool alertWithTitle:@"订单认领" content:@"是否确定认领该订单？" sureAction:^{
            [RequestUtil withPOST:@"/doctor/order/claim" parameters:@{@"order_id":orderid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
                if (code == 200) {
//                    [weakSelf requestData];
                    [weakSelf.dataArray removeObjectAtIndex:index];
                    [weakSelf.tableView reloadData];
                    if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(needRefressData)]) {
                        [weakSelf.delegate needRefressData];
                    }
                }else{
                    [TooltipView showMessage:message offset:0];
                }
            } error:^(NSError *error) {
                
            }];
        }];
    }
}

-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(15), Screen_W-viewPix(32), viewPix(34))];
        _searchTF.backgroundColor = [UIColor whiteColor];
        _searchTF.tintColor = [UIColor colorWithString:@"#555555"];
        _searchTF.textColor = [UIColor colorWithString:@"#555555"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.keyboardType = UIKeyboardTypeDefault;
        _searchTF.placeholder = @"输入患者姓名";
        _searchTF.cornerRidus = viewPix(16);
        _searchTF.font = LGFont(17);
        [_searchTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
        _searchTF.leftView = [self leftView];
    }
    return _searchTF;
}

-(UIView *)leftView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(35), viewPix(34))];
    UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(9), viewPix(16), viewPix(16))];
    iconView.image = [UIImage imageNamed:@"searchGray16"];
    [baseView addSubview:iconView];
    return baseView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(55), Screen_W, Screen_H-topBarHeight-viewPix(85)) style:UITableViewStylePlain];
        [_tableView registerClass:[LGOrderConfirmTableCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = viewPix(156);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    }
    return _tableView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
